package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Util;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

public class UserServiceImpl implements UserService {
    @Override
    public PageInfo<User> findByPage(int currentPage, int pageSize) {
        PageInfo<User> page = null;
        SqlSession sqlSession = null;
        try {
            //1 通过MybatisUtil工厂类获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2 通过MybatisUtil工厂类获取dao接口的代理对象
            UserDao mapper = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //3 执行操作释放资源
            //3.1 在查询所有之前设置分页参数
            PageHelper.startPage(currentPage,pageSize);
            List<User> list = mapper.findAll();
             page = new PageInfo<>(list);
            //MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return page;
    }

    @Override
    public void save(User cept) {
        SqlSession sqlSession=null;
        try {
             sqlSession = MybatisUtil.getSqlSession();
            UserDao mapper = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //生成一个唯一的id，保存到cept
            String id = UUID.randomUUID().toString().replace("-", "");
            cept.setId(id);
            mapper.save(cept);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public User findById(String id) {
        User cept = null;
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            UserDao mapper = MybatisUtil.getMapper(sqlSession, UserDao.class);

            cept =  mapper.findById(id);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
        return cept;
    }

    @Override
    public void update(User cept) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            UserDao mapper = MybatisUtil.getMapper(sqlSession, UserDao.class);

            mapper.update(cept);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            UserDao mapper = MybatisUtil.getMapper(sqlSession, UserDao.class);


            Stream.of(ids).forEach(id->{
                //删除用户和角色之间的对应关系
                mapper.deleteRoleAssociationByUserId(id);
                //删除用户信息
                mapper.delete(id);
            });
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public List<User> findAll() {
       List<User>  list = null;
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            UserDao mapper = MybatisUtil.getMapper(sqlSession, UserDao.class);

            list =  mapper.findAll();
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public void updateUserAndRoles(String userId, String[] checkedRoleIds) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            UserDao mapper = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //3.1 根据用户id删除绑定的角色，操作ss_role_user
            mapper.deleteUserAndRoles(userId);
            Stream.of(checkedRoleIds).forEach(role->{
                mapper.saveUserAndRoles(userId,role);
            });
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    /**
     * 用户登录
      * @param username
     * @param password
     * @return User
     */
    @Override
    public User login(String username, String password) {
        SqlSession sqlSession=null;
        User user = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            UserDao mapper = MybatisUtil.getMapper(sqlSession, UserDao.class);
            password = MD5Util.md5(password);
             user = mapper.findByEmailAndPassword(username, password);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return user;
    }
}
